package com.example.shishaolong.warehousemanager;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.example.shishaolong.warehousemanager.common.DateUtil;
import com.example.shishaolong.warehousemanager.dao.AdminDao;
import com.example.shishaolong.warehousemanager.entity.AdminEntity;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;
import java.util.Random;

import static org.junit.Assert.assertEquals;

/**
 * 测试：管理员数据访问层
 *
 * @author 时少龙
 */
@RunWith(AndroidJUnit4.class)
public class AdminTest {

    /**
     * 上下文环境
     */
    private Context context;

    @Before
    public void init() {
        //初始化上下文环境    ****************安卓测试中的起点****************
        context = InstrumentationRegistry.getTargetContext();
    }

    @Test
    public void batchAdd() {
        System.out.println("批量添加管理员测试！");
        for (int i = 0; i < 10; i++) {
            AdminDao adminDao = new AdminDao(context);
            AdminEntity adminEntity = new AdminEntity();
            adminEntity.setName("时少龙"+getRandomInt());
            adminEntity.setPassword("123456");
            adminEntity.setSex(((getRandomInt()%2==0)?1:2));  //1：男  2.女
            adminEntity.setLevel(((getRandomInt()%2==0)?1:2));//1:超管  2.普管
            adminEntity.setAddDate(DateUtil.getCurrentDateStr());
            adminEntity.setModifyDate(DateUtil.getCurrentDateStr());
            adminDao.add(adminEntity);
        }
    }

    @Test
    public void addAdmin() {
        System.out.println("测试添加管理员");
        AdminDao adminDao = new AdminDao(context);
        AdminEntity adminEntity = new AdminEntity();
        adminEntity.setName("时少龙");
        adminEntity.setPassword("123456");
        adminEntity.setSex(1);
        adminEntity.setLevel(1);
        adminEntity.setAddDate(DateUtil.getCurrentDateStr());
        adminEntity.setModifyDate(DateUtil.getCurrentDateStr());
        adminDao.add(adminEntity);
    }

    @Test
    public void findAllAdmin() {
        System.out.println("测试查询所有的数据");
        AdminDao adminDao = new AdminDao(context);
        List<AdminEntity> adminEntityList = adminDao.findAll();
        for (AdminEntity adminEntity : adminEntityList) {
            System.out.println(adminEntity);
        }
    }

    @Test
    public void deleteAll() {
        System.out.println("测试删除全部开始：");
        AdminDao adminDao = new AdminDao(context);
        adminDao.deleteAll();
    }

    @Test
    public void updateAdmin() {
        System.out.println("测试修改");
        AdminDao adminDao = new AdminDao(context);
        AdminEntity adminEntity = adminDao.findById(7);
        adminEntity.setName("李龙");
        adminDao.update(adminEntity);
    }

    @Test
    public void deleteById() {
        System.out.println("测试删除单个管理员");
        AdminDao adminDao = new AdminDao(context);
        adminDao.delete(6);
    }

    /**
     * 返回15-25 的随机数,批量添加时使用
     * @return
     */
    public int getRandomInt() {
        return new Random().nextInt(25-15)+15;
    }
}
